import { Logger } from "../../engine/Logger";
import ViewBase from "../../engine/ViewBase";
import rolling_lottery from "./rolling_lottery/rolling_lottery";

const { ccclass, property } = cc._decorator;

@ccclass
export default class ViewRollingLottery extends ViewBase {

    private _rolling_lottery_os: rolling_lottery[] = [];
    // 节点数量
    childLength: number = 8;

    onEnable(): void {
        super.onEnable()
        let machine: cc.Node = this.findChild(this.node, "machine")
        machine.children.forEach((value: cc.Node, index: number) => {
            let rlSrc = value.getChildByName('rolling').getComponent(rolling_lottery)
            this._rolling_lottery_os.push(rlSrc);
            Logger.log("_rolling_lottery_os=", this._rolling_lottery_os)
            this._rolling_lottery_os[index].reset();
        });

    }


    /* ***************按钮事件*************** */
    public btn_mess(): void {
        this._rolling_lottery_os.forEach(v1_o => {
            v1_o.random_order();
        });
    }

    public btn_must(): void {
        if (this._rolling_lottery_os.filter(v1_o => v1_o.scroll_b).length) {
            Logger.warn("动作未结束");
            return;
        }
        const rand:number = Math.floor(Math.random() * (this.childLength - 1));
        Logger.log("btn_must===", rand)
        this._rolling_lottery_os.forEach(v1_o=> {
            const old_speed_n = v1_o.max_speed_n;
            // 修改速度让其更加真实
            v1_o.max_speed_n += Math.floor(Math.random() * 11) - 5;
            Logger.log("rand number is =>", rand);
            v1_o.scroll(rand, {
                "rebound_finish_cb_f": ()=> {
                    v1_o.max_speed_n = old_speed_n;
                }
            });
        });

    }
}
